package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.utils.redis.JedisUtil;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.constant.OrderType;
import com.nuanshui.heatedloan.credit.facade.ApplyOrderService;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.pay.OrderPayStateService;
import com.nuanshui.heatedloan.credit.pay.PayInformPara;
import com.nuanshui.heatedloan.credit.pay.constant.OrderPayState;
import com.nuanshui.heatedloan.credit.vo.CustomerBankCardInfoVO;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by wangxw on 2017/8/1.
 */

@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_PAY_STATUS)
public class PayStatusHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger("PayStatusHandler");

    @Autowired
    private OrderPayStateService orderPayStateService;

    @Autowired
    private PayOrderInfoService payOrderInfoService;

    @Autowired
    private ApplyOrderService applyOrderService;


    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private DubboCacheManager dubboCacheManager;



    public void onMessage(Message message, Channel channel ) throws Exception{
        try{
            logger.info("=====开始进入订单状态处理消息队列======");
            String msg = new String(message.getBody());
            PayInformPara payInformPara= JSON.parseObject(msg,PayInformPara.class);
            PayOrderInfoVO payOrderInfoVO=payOrderInfoService.loadVOByOrderNo(payInformPara.getOrderId());
            int payType=payInformPara.getPayType();
            String orderId=payInformPara.getOrderId();
            int orderStatus=payOrderInfoVO.getOrderStatus();
            logger.info("=====处理订单的订单号订单支付类型为"+payInformPara.getOrderId()+"======"+payInformPara.getPayType()+"======");
            int payStatus=orderPayStateService.getOrderState(payInformPara.getOrderId(),payInformPara.getPayType());
            logger.info("=====支付订单的返回状态为"+payStatus+"=======================");
             //代付
            if(com.nuanshui.heatedloan.credit.pay.PayType.TYPE_PAY==payType){
                //开始打款
                if(OrderPayState.STATE_PROCESSING==payStatus){
                    payOrderInfoService.updateStatusAndTime(1,OrderType.LENDINGORDER,orderId,null);
                }
                //打款成功
                else if(OrderPayState.STATE_SUCCESS==payStatus){
                    payOrderInfoService.updateStatusAndTime(2,OrderType.NOREIMBURSEMENT,orderId,null);
                    payInformPara.setState(payStatus);
                    applyOrderService.dealPayAfter(payInformPara);

                }
                //打款失败
                else if(OrderPayState.STATE_FAIL==payStatus){
                    payOrderInfoService.updateStatusAndTime(1,OrderType.LENDFAIL,orderId,null);
                    payInformPara.setState(payStatus);
                    applyOrderService.dealPayAfter(payInformPara);
                }
            }
            //还款
            else if(com.nuanshui.heatedloan.credit.pay.PayType.TYPE_REPAY==payType){
                //逾期贷还款
                if(OrderPayState.STATE_SUCCESS==payStatus){
                    CustomerBankCardInfoVO customerBankCardInfoVO=(CustomerBankCardInfoVO)dubboCacheManager.
                            get(DubboCacheRegion.CACHE_REPAY_BANK_CARD_NO,payOrderInfoVO.getOrderNo()+payOrderInfoVO.getUserId());
                   // String value=jedisUtil.get(payOrderInfoVO.getOrderNo()+payOrderInfoVO.getUserId());
                    //CustomerBankCardInfoVO customerBankCardInfoVO=JSON.parseObject(value,CustomerBankCardInfoVO.class);
                    if(payOrderInfoVO.getOverdueDays()>0){
                        payOrderInfoService.updateStatusAndTime(3,OrderType.LATEPAYMENT,orderId,customerBankCardInfoVO.getMixCardNo());
                    }else{
                        payOrderInfoService.updateStatusAndTime(3,OrderType.REIMBURSEMENT,orderId,customerBankCardInfoVO.getMixCardNo());
                    }
                    //jedisUtil.del(payOrderInfoVO.getOrderNo()+payOrderInfoVO.getUserId());
                    dubboCacheManager.evictCache(DubboCacheRegion.CACHE_REPAY_BANK_CARD_NO,payOrderInfoVO.getOrderNo()+payOrderInfoVO.getUserId());
                    payInformPara.setState(payStatus);
                    applyOrderService.dealPayAfter(payInformPara);
                }//打款中
                else if(OrderPayState.STATE_PROCESSING==payStatus){
                    payOrderInfoService.updateStatusAndTime(1,OrderType.REPAYING,orderId,null);
                }//打款失败
                else if(OrderPayState.STATE_FAIL==payStatus){
                    if(payOrderInfoVO.getOverdueDays()>0){
                        payOrderInfoService.updateStatusAndTime(1,OrderType.OVERDUEPAYMENT,orderId,null);
                    }else{
                        payOrderInfoService.updateStatusAndTime(1,OrderType.NOREIMBURSEMENT,orderId,null);
                    }

                }
            }
            logger.info("======完成订单状态处理=========");
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}
